//
//  TMGMineViewController.m
//  TMGold
//
//  Created by apple on 2017/11/8.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGMineViewController.h"

@interface TMGMineViewController ()<ChartViewDelegate,EaseMessageViewControllerDelegate,EaseMessageViewControllerDataSource>
@property(nonatomic, strong) LineChartView                     *chartView;
@property(nonatomic, strong) NSArray                            *options;
@end

@implementation TMGMineViewController

#pragma mark --vc生命周期

- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //    static dispatch_once_t onceToken;
    //   dispatch_once(&onceToken, ^{
    //        [self showLoginVc];
    //    });
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self chartViewData];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    self.options = @[
                     @{@"key": @"toggleValues", @"label": @"Toggle Values"},
                     @{@"key": @"toggleFilled", @"label": @"Toggle Filled"},
                     @{@"key": @"toggleCircles", @"label": @"Toggle Circles"},
                     @{@"key": @"toggleCubic", @"label": @"Toggle Cubic"},
                     @{@"key": @"toggleHorizontalCubic", @"label": @"Toggle Horizontal Cubic"},
                     @{@"key": @"toggleIcons", @"label": @"Toggle Icons"},
                     @{@"key": @"toggleStepped", @"label": @"Toggle Stepped"},
                     @{@"key": @"toggleHighlight", @"label": @"Toggle Highlight"},
                     @{@"key": @"animateX", @"label": @"Animate X"},
                     @{@"key": @"animateY", @"label": @"Animate Y"},
                     @{@"key": @"animateXY", @"label": @"Animate XY"},
                     @{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
                     @{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
                     @{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
                     @{@"key": @"toggleData", @"label": @"Toggle Data"},
                     ];
    
    self.chartView = [[LineChartView alloc] initWithFrame:CGRectMake(10, 100, UIScreenWidth - 20, 300)];
    _chartView.delegate = self;
    _chartView.chartDescription.enabled = NO;
    self.chartView.xAxis.drawGridLinesEnabled = NO;//不绘制网络线
    _chartView.dragEnabled = YES;
    [_chartView setScaleEnabled:YES];
    _chartView.pinchZoomEnabled = YES;
    _chartView.drawGridBackgroundEnabled = NO;
    
    // x-axis limit line
    ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
    llXAxis.lineWidth = 4;
    llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
    llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
    llXAxis.valueFont = [UIFont systemFontOfSize:10.f];
    
    _chartView.xAxis.enabled = NO;
    
    ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"];
    ll1.lineWidth = 4.0;
    ll1.lineDashLengths = @[@5.f, @5.f];
    ll1.labelPosition = ChartLimitLabelPositionRightTop;
    ll1.valueFont = [UIFont systemFontOfSize:10.0];
    
    ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
    ll2.lineWidth = 4.0;
    ll2.lineDashLengths = @[@5.f, @5.f];
    ll2.labelPosition = ChartLimitLabelPositionRightBottom;
    ll2.valueFont = [UIFont systemFontOfSize:10.0];
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    [leftAxis removeAllLimitLines];
    leftAxis.axisMaximum = 276.4;
    leftAxis.axisMinimum = 275.5;
    leftAxis.gridLineDashLengths = @[@5.f, @5.f];
    leftAxis.drawZeroLineEnabled = NO;
    leftAxis.drawLimitLinesBehindDataEnabled = YES;
    _chartView.rightAxis.enabled = NO;
    _chartView.legend.enabled = NO;
    [self.view addSubview:self.chartView];
    
    [_chartView animateWithXAxisDuration:2.5];
    
    
    __weak typeof(self) weakSelf =  self;
    STButton * liaoButton = [[STButton alloc] initWithFrame:CGRectMake(50, _chartView.st_bottom + 30, 100, 100)
                                                      title:@"客服"
                                                 titleColor:[UIColor flatPinkColor]
                                                  titleFont:15
                                               cornerRadius:2
                                            backgroundColor:[UIColor lightGrayColor]
                                            backgroundImage:nil
                                                      image:nil];
    [self.view addSubview:liaoButton];
    [liaoButton setClicAction:^(UIButton *sender) {
        [weakSelf IMTest];
    }];
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}
#pragma mark --Private Method
- (void)chartViewData{
    NSArray * array = [self createGoldData];
    NSMutableArray *nowArray = [NSMutableArray new];
    
    for (NSDictionary * dic in array) {
        CGFloat x = [dic[@"time"] floatValue];
        CGFloat y = [dic[@"money"] floatValue];
        ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:x y:y];
        [nowArray addObject:entry];
    }
    LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithValues:nowArray label:@""];
    set1.drawIconsEnabled = NO;
    [set1 setColor:UIColor.blackColor];
    [set1 setCircleColor:UIColor.blackColor];
    set1.lineWidth = 1.0;
    set1.drawCircleHoleEnabled = NO;
    set1.drawValuesEnabled = NO;
    set1.circleRadius = 0;
    set1.valueFont = [UIFont systemFontOfSize:9.f];
    set1.highlightEnabled = YES;//选中拐点,是否开启高亮效果(显示十字线)
    set1.highlightColor = [UIColor flatPinkColor];// 十字线颜色
    set1.formLineWidth = 1.0;
    set1.formSize = 15.0;
    NSArray *gradientColors = @[
                                (id)FlatWhiteDark.CGColor,
                                (id)FlatWhite.CGColor
                                ];
    CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
    
    set1.fillAlpha = 0.5;
    set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
    set1.drawFilledEnabled = YES;
    set1.label = @"";
    
    CGGradientRelease(gradient);
    LineChartData *data = [[LineChartData alloc] initWithDataSets:@[set1]];
    self.chartView.data = data;
}
- (void)chartViewDatas{
    NSInteger count = self.options.count;
    
    NSMutableArray *values = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double val = arc4random_uniform(80) + 3;
        [values addObject:[[ChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"icon"]]];
    }
    
    LineChartDataSet *set1 = nil;
    if (_chartView.data.dataSetCount > 0)
    {
        set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
        set1.values = values;
        [_chartView.data notifyDataChanged];
        [_chartView notifyDataSetChanged];
    }
    else
    {
        set1 = [[LineChartDataSet alloc] initWithValues:values label:@"DataSet 1"];
        
        set1.drawIconsEnabled = NO;
        
        set1.lineDashLengths = @[@5.f, @2.5f];
        set1.highlightLineDashLengths = @[@5.f, @2.5f];
        [set1 setColor:UIColor.blackColor];
        [set1 setCircleColor:UIColor.blackColor];
        set1.lineWidth = 1.0;
        set1.circleRadius = 3.0;
        set1.drawCircleHoleEnabled = NO;
        set1.valueFont = [UIFont systemFontOfSize:9.f];
        set1.formLineDashLengths = @[@5.f, @2.5f];
        set1.formLineWidth = 1.0;
        set1.formSize = 15.0;
        
        NSArray *gradientColors = @[
                                    (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
                                    (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
                                    ];
        CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
        
        set1.fillAlpha = 1.f;
        set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
        set1.drawFilledEnabled = YES;
        
        CGGradientRelease(gradient);
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
        _chartView.data = data;
    }
}
- (void)showLoginVc{
//    TMGNavgationViewController * loginNav = [[TMGNavgationViewController alloc] initWithRootViewController:[TMGInputPhoneViewController new]];
//    [self presentViewController:loginNav animated:YES completion:nil];
}
- (NSArray*)createGoldData{
    NSMutableArray * dataSouce = [NSMutableArray new];
    for (int i = 0; i < 24; i++) {
        for (int k = 0; k < 4; k++) {
            NSString * hour = [NSString stringWithFormat:@"%d",i];
            hour = hour.length < 2?[NSString stringWithFormat:@"0%@",hour]:hour;
            
            NSString * min = [NSString stringWithFormat:@"%d",15*k];
            min = min.length < 2?[NSString stringWithFormat:@"0%@",min]:min;
            
            NSString * time = [NSString stringWithFormat:@"%@.%@",hour,min];
            
            CGFloat goldMoney = 275.9 + (CGFloat)(arc4random()%30)/100;
            if (i == 10 || i == 11 || i == 12|| i == 13 ) {
                goldMoney = 276.0;
            }
            [dataSouce addObject:@{@"time":time,@"money":@(goldMoney)}];
            
        }
    }
    return dataSouce;
}
- (void)IMTest{
    EMClient * client = [EMClient sharedClient];
    NSString * userName =@"564826425";
    NSString * pwd = @"123456";
    BOOL isSuccess = [client registerWithUsername:userName password:pwd];
    
    [client loginWithUsername:userName password:pwd completion:^(NSString *aUsername, EMError *aError) {
        //单聊
        if (!aError) {
            EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:serviceCustomPhpne conversationType:EMConversationTypeChat];
            chatController.delegate = self;
            chatController.dataSource = self;
            [self.navigationController pushViewController:chatController animated:YES];
        }

    }];
}
#pragma mark ---EaseMessageViewControllerDataSource
-(id<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController
                          modelForMessage:(EMMessage *)message{
    
    id <IMessageModel> obj = nil;
    obj = [[EaseMessageModel alloc] initWithMessage:message];
    obj.avatarImage = [UIImage imageNamed:@"1"];
    obj.nickname = @"sxl";
    return obj;
}
@end

